package info.androidhive.pratap;

import info.androidhive.pratap.models.Album;
import info.androidhive.volleyexamples.R;
import info.androidhive.volleyexamples.app.AppController;

import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AlbumsAdapter extends BaseAdapter {

	Context context;

	List<Album> albums;
	ImageLoader imageLoader;

	public AlbumsAdapter(Context context1, List<Album> album1) {
		this.context = context1;
		this.albums = album1;
		imageLoader = AppController.getInstance().getImageLoader();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return albums.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return albums.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return albums.indexOf(getItem(position));
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ProductViewHolder holder = null;

		LayoutInflater rowInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

		if (convertView == null) {
			convertView = rowInflater
					.inflate(R.layout.album_listview_row, null);

			holder = new ProductViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.artist = (TextView) convertView.findViewById(R.id.artist);
			holder.duration = (TextView) convertView
					.findViewById(R.id.duration);
			holder.list_image = (NetworkImageView) convertView
					.findViewById(R.id.list_image);
			convertView.setTag(holder);

		} else {
			holder = (ProductViewHolder) convertView.getTag();
		}

		Album item = (Album) getItem(position);

		holder.title.setText(item.getTitle());
		holder.artist.setText(item.getArtist());

		holder.duration.setText(item.getDuration());

		holder.list_image.setImageUrl(item.getThumbUrl(), imageLoader);

		return convertView;
	}

	public class ProductViewHolder {

		TextView title;

		TextView artist;

		TextView duration;

		NetworkImageView list_image;

	}

}
